home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / wv2 / sharedptr.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-06-12  |  4.3 KB  |  145 lines

  1. /* This file is part of the KDE libraries
  2.    Copyright (c) 1999 Waldo Bastian <bastian@kde.org>
  3.    Copyright (c) 2001-2003 Werner Trobin <trobin@kde.org>
  4.  
  5.    This library is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Library General Public
  7.    License version 2 as published by the Free Software Foundation.
  8.  
  9.    This library is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.    Library General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU Library General Public License
  15.    along with this library; see the file COPYING.LIB.  If not, write to
  16.    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17.    Boston, MA 02111-1307, USA.
  18. */
  19. #ifndef SharedPTR_H
  20. #define SharedPTR_H
  21.  
  22. #include "dllmagic.h"
  23.  
  24. namespace wvWare {
  25.  
  26. /**
  27.  * Reference counting for shared objects.  If you derive your object
  28.  * from this class, then you may use it in conjunction with
  29.  * @ref SharedPtr to control the lifetime of your object.
  30.  *
  31.  * Specifically, all classes that derive from Shared have an internal
  32.  * counter keeping track of how many other objects have a reference to
  33.  * their object.  If used with @ref SharedPtr, then your object will
  34.  * not be deleted until all references to the object have been
  35.  * released.
  36.  *
  37.  * You should probably not ever use any of the methods in this class
  38.  * directly -- let the @ref SharedPtr take care of that.  Just derive
  39.  * your class from Shared and forget about it.
  40.  *
  41.  * @author Waldo Bastian <bastian@kde.org>
  42.  */
  43. class WV2_DLLEXPORT Shared {
  44. public:
  45.    /**
  46.     * Standard constructor.  This will initialize the reference count
  47.     * on this object to 0
  48.     */
  49.    Shared() : count( 0 ) { }
  50.  
  51.    /**
  52.     * Copy constructor.  This will @em not actually copy the objects
  53.     * but it will initialize the reference count on this object to 0
  54.     */
  55.    Shared( const Shared & ) : count( 0 ) { }
  56.  
  57.    /**
  58.     * Overloaded assignment operator
  59.     */
  60.    Shared &operator=( const Shared & ) { return *this; }
  61.  
  62.    /**
  63.     * Increases the reference count by one
  64.     */
  65.    void _Shared_ref() const { count++; }
  66.  
  67.    /**
  68.     * Releases a reference (decreases the reference count by one).  If
  69.     * the count goes to 0, this object will delete itself
  70.     */
  71.    void _Shared_deref() const { if (!--count) delete this; }
  72.  
  73.    /**
  74.     * Return the current number of references held
  75.     *
  76.     * @return Number of references
  77.     */
  78.    int _Shared_count() const { return count; }
  79.  
  80. protected:
  81.    virtual ~Shared() { }
  82. private:
  83.    mutable int count;
  84. };
  85.  
  86. /**
  87.  * Can be used to control the lifetime of an object that has derived
  88.  * @ref Shared. As long a someone holds a SharedPtr on some Shared
  89.  * object it won't become deleted but is deleted once its reference
  90.  * count is 0.  This struct emulates C++ pointers perfectly. So just
  91.  * use it like a simple C++ pointer.
  92.  *
  93.  * @author Waldo Bastian <bastian@kde.org>
  94.  */
  95. template< class T >
  96. struct SharedPtr
  97. {
  98. public:
  99.   SharedPtr()
  100.     : ptr(0) { }
  101.   SharedPtr( T* t )
  102.     : ptr(t) { if ( ptr ) ptr->_Shared_ref(); }
  103.   SharedPtr( const SharedPtr& p )
  104.     : ptr(p.ptr) { if ( ptr ) ptr->_Shared_ref(); }
  105.  
  106.   ~SharedPtr() { if ( ptr ) ptr->_Shared_deref(); }
  107.  
  108.   SharedPtr<T>& operator= ( const SharedPtr<T>& p ) {
  109.     if ( ptr == p.ptr ) return *this;
  110.     if ( ptr ) ptr->_Shared_deref();
  111.     ptr = p.ptr;
  112.     if ( ptr ) ptr->_Shared_ref();
  113.     return *this;
  114.   }
  115.   SharedPtr<T>& operator= ( T* p ) {
  116.     if ( ptr == p ) return *this;
  117.     if ( ptr ) ptr->_Shared_deref();
  118.     ptr = p;
  119.     if ( ptr ) ptr->_Shared_ref();
  120.     return *this;
  121.   }
  122.   bool operator== ( const SharedPtr<T>& p ) const { return ( ptr == p.ptr ); }
  123.   bool operator!= ( const SharedPtr<T>& p ) const { return ( ptr != p.ptr ); }
  124.   bool operator== ( const T* p ) const { return ( ptr == p ); }
  125.   bool operator!= ( const T* p ) const { return ( ptr != p ); }
  126.   bool operator!() const { return ( ptr == 0 ); }
  127.   operator T*() const { return ptr; }
  128.  
  129.   T* data() { return ptr; }
  130.   const T* data() const { return ptr; }
  131.  
  132.   const T& operator*() const { return *ptr; }
  133.   T& operator*() { return *ptr; }
  134.   const T* operator->() const { return ptr; }
  135.   T* operator->() { return ptr; }
  136.  
  137.   int count() const { return ptr->_Shared_count(); } // for debugging purposes
  138. private:
  139.   T* ptr;
  140. };
  141.  
  142. } // namespace wvWare
  143.  
  144. #endif
  145.